package com.rtsp.poe.rtsplive555;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.easydarwin.blogdemos.AudioManagerUtils;

/**
 * @author liyaotang
 * @date 2019/1/23
 */
public class SecondActivity extends Activity {

    private PreviewPresenter mPreviewPresenter;
//    private PreviewPresenter mPreviewPresenter2;

    /** 预览渲染的View */
    private SurfaceView mSurfaceView;
//    private SurfaceView mSurfaceView2;
//
//    private String RTSP_ADDRESS = "rtsp://admin:spon123456@10.1.4.168:554/h264/ch2/main/av_stream";
//    private String RTSP_ADDRESS_2 = "rtsp://admin:admin123@192.168.4.233:554";
//    private String RTSP_ADDRESS_3 = "rtsp://admin:admin123@10.1.0.15";
//    private String RTSP_ADDRESS = "rtsp://admin:admin_123@10.1.4.96/media/video1";
//    private String RTSP_ADDRESS = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
//    private String RTSP_ADDRESS = "rtsp://admin:admin123@192.168.4.233:554";
//    private String RTSP_ADDRESS = "rtsp://admin:Rdc070@192.168.1.80/2";
    private String RTSP_ADDRESS = "rtsp://admin:Rdc070#*@192.168.1.101/audiotracker_1";
    private boolean isStreamForceTcp = false;
//    private boolean isStreamForceTcp = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initView();
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
                && !mNotificationManager.isNotificationPolicyAccessGranted()) {
            Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
            startActivityForResult(intent,11);
        } else {
            AudioManagerUtils.getInstance(this).setStreamVolume(50);
        }
        mPreviewPresenter = new PreviewPresenter(this, new PreviewView() {
            @Override
            public void showInfo(String info) {
                Toast.makeText(SecondActivity.this, info, Toast.LENGTH_SHORT).show();
            }
        });
//        mPreviewPresenter2 = new PreviewPresenter(this, new PreviewView() {
//            @Override
//            public void showInfo(String info) {
////                Toast.makeText(SecondActivity.this, info, Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 11){
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (mNotificationManager.isNotificationPolicyAccessGranted()) {
                AudioManagerUtils.getInstance(this).setStreamVolume(50);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initView() {
        mSurfaceView = findViewById(R.id.surface_view);
//        mSurfaceView2 = findViewById(R.id.surface_view2);
        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mPreviewPresenter.setSurfaceView(mSurfaceView);
                mPreviewPresenter.setPreviewAddress(RTSP_ADDRESS, isStreamForceTcp);
                mPreviewPresenter.startPreview();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mPreviewPresenter.stopPreview();
            }
        });
        /*mSurfaceView2.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
//                mPreviewPresenter2.setPreviewAddress(RTSP_ADDRESS_2);
//                mPreviewPresenter2.setSurfaceView(mSurfaceView2);
//                mPreviewPresenter2.startPreview();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
//                mPreviewPresenter2.stopPreview();
            }
        });*/

        EditText et = findViewById(R.id.et_url);
        et.setText(RTSP_ADDRESS);
    }

    public void startPreview(View view) {
        EditText ed = findViewById(R.id.et_url);
        mPreviewPresenter.setPreviewAddress(RTSP_ADDRESS, isStreamForceTcp);
        mPreviewPresenter.setSurfaceView(mSurfaceView);
        mPreviewPresenter.startPreview();
//        mPreviewPresenter2.setPreviewAddress(RTSP_ADDRESS_2);
//        mPreviewPresenter2.setSurfaceView(mSurfaceView2);
//        mPreviewPresenter2.startPreview();
    }

    public void stopPreview(View view) {
        mPreviewPresenter.stopPreview();
//        mPreviewPresenter2.stopPreview();
    }

    public void exitApp(View view) {
        finish();
    }
}
